﻿using System.Collections.Generic;
using System.Diagnostics;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Integration.Tests
{
    [TestClass()]
    public class CsvFileProcessorTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void CsvFileProcessorProcessTest1()
        {
            List<string> headers = new List<string>();
            List<List<string>> rows = new List<List<string>>();

            CsvFileProcessor processor = new CsvFileProcessor
            {
                Path = @"..\..\..\Integration.Tests\TestFiles\CsvTestFile.csv",
                HasHeader = true,
                OnHeaderFetched = (row, rowNumber, messages) =>
                {
                    headers.AddRange(row);

                    return true;
                },
                OnRowFetched = (row, rowNumber, messages) =>
                {
                    List<string> r = new List<string>();
                    r.AddRange(row);

                    rows.Add(r); // Copy the row into the buffer

                    return true;
                },
                OnEmptyRow = (rowNumber, messages) =>
                {
                    messages.Add(new FileProcessorMessage
                    {
                        RowNumber = rowNumber,
                        Text = "Empty row"
                    });

                    return true;
                }
            };

            processor.Process();

            Assert.AreEqual(4, headers.Count);

            Assert.AreEqual("ID", headers[0]);
            Assert.AreEqual("DATE", headers[1]);
            Assert.AreEqual("NAME", headers[2]);
            Assert.AreEqual("COMMENTS", headers[3]);

            Assert.AreEqual(3, rows.Count);

            Assert.AreEqual(7, processor.ProcessedRows);

            Assert.AreEqual(4, rows[0].Count);

            Assert.AreEqual("111", rows[0][0]);
            Assert.AreEqual("2008-07-12 12:12:00", rows[0][1]);
            Assert.AreEqual("Doe, Joe", rows[0][2]);
            Assert.AreEqual(
@"Sent an email: return email - he wrote ""Thanks.  Will let you know.""
(Recorded 07/12/2008 12:12 pm)", 
                rows[0][3]);

            foreach (FileProcessorMessage message in processor.Messages)
            {
                Debug.WriteLine(message.ToString());
            }

            Debug.Write("Time elapsed: " + processor.ElapsedTime);
        }
    }
}
